﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Kernel;

namespace SppdProjectHelper
{
    public partial class FormMain : FormBase,IControlEx
    {
        public FormMain()
            :base()
        {
            InitializeComponent();
            
            this._pad_main.Dock = DockStyle.Fill;   //将该控件填充显示
            this.tableLayoutPanel_main.Controls.Add(_pad_main, 0, 1);

            this.AddExControl(this);

            /*尝试打开当前DWG文档下的SPM文档*/
            funcTryToOpenPrjFileOfActDoc();

            this.InitAllExControls();
            SysMisc.LogHelper.LogUpdateEvent += LogHelper_LogUpdateEvent;
        }

        private void LogHelper_LogUpdateEvent(SysMisc.LogHelper.LogType typ, Type t, string log)
        {
            //Misc.WriteToTxtFile(log+"\r\n");
            Misc.Alert(log);
        }

        private Project _prj = new Project();
        private pad_main _pad_main = new pad_main();

        #region 实现IControlEx接口
        void IControlEx.Init() { }
        void IControlEx.Load() { UI_Load(); }
        void IControlEx.Update() { UI_Update(); }
        void IControlEx.Save() { Save(); }
        bool IControlEx.Updated { get; }
        #endregion

        public void UI_Load()
        {
            _pad_main.OnCreate();
        }

        public void UI_Update()
        {
            funcUpdateDataToUI();

            //this.UpdateAllExControls();
        }

        public void Save()
        {
            funcUpdateDataFromUI();
        }

        private void funcUpdateDataToUI()
        {
        }

        private void funcUpdateDataFromUI()
        {
        }

        private void toolStripButton_Click(Object sender, EventArgs ex)
        {
            ToolStripButton tsb = (ToolStripButton)sender;
            switch (tsb.Text)
            {
                case "打开工程":
                    {
                        OpenFileDialog ofd = new OpenFileDialog();
                        ofd.Filter = "spm文件(*.spm) | *.spm";//文件类型
                        DialogResult dr = ofd.ShowDialog();
                        if( dr == DialogResult.OK)
                        {
                            //_prj.ProjectConfig = SysMisc.Serialization.DeSerializeFromXml<ProjectConfig>(ofd.FileName);

                            this._prj = new Project(ofd.FileName);
                            //this._pfv.Project = this._prj;
                            //this._cv.Project = this._prj;
                            this.UpdateAllExControls();
                        }
                    }
                    break;
                case "保存工程":
                    {
                        this.SaveAllExControls();
                        if( false == _prj.Save())
                        {
                            Misc.Alert(_prj.ErrorText);
                        }else
                        {
                            Misc.Alert("保存成功");
                            _prj.IsUpdated = false;
                        }
                    }
                    break;
                case "导入图框":
                    {
                        string str_dir="";
                        try
                        {
                            str_dir = Misc.GetAppPath() + @"\Frames";
                            //打开图框模板文件夹
                            System.Diagnostics.Process.Start( str_dir );
                        }
                        catch
                        {
                            Misc.Alert(string.Format("打开文件夹“{0}”失败！",str_dir));
                        }
                    }
                    break;
            }
        }

        private void toolStripMenuItem_Click(Object sender, EventArgs ex)
        {
            ToolStripMenuItem tsb = (ToolStripMenuItem)sender;
            switch (tsb.Text)
            {
                case "导入底图":
                    {
                        using (Form_BaseMapLead frm_bml = new Form_BaseMapLead())
                        {
                            frm_bml.ShowInTaskbar = false;
                            //frm_bml.ProjectConfig = this.Project.ProjectConfig;
                            DialogResult dr = Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(frm_bml);
                        }
                    }
                    break;
            }

        }

        private void Button_Click(Object sender, EventArgs ex)
        {
            Button btn = (Button)sender;
            switch (btn.Text)
            {
                case "确认修改":
                    {
                        funcUpdateDataFromUI();
                        Misc.Alert("保存成功！");

                    }
                    break;
                case "开始打印":
                    {
                        Autodesk.AutoCAD.PlottingServices.PlotProgressDialog ppd = new Autodesk.AutoCAD.PlottingServices.PlotProgressDialog(false,1,true);
                        ppd.OnBeginPlot();
                    }
                    break;
            }
        }

        /// <summary>
        /// 尝试打开当前活动文档下的工程文件
        /// </summary>
        private void funcTryToOpenPrjFileOfActDoc()
        {
            if( true == _prj.TryToOpenPrjFileOfActDoc())
            {
                this._pad_main.Project = _prj;
                this.UpdateAllExControls();
            }
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            //加载系统配置文件
            if( false ==  _prj.LoadSystemConfig())
            {
                Misc.Alert("读取系统配置文件失败！点击确定退出。");
                this.Close();
            }

            this.LoadAllExControls();
        }

        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(_prj.IsUpdated == true)
            {
                DialogResult dr = MessageBox.Show("检测到工程文件有未被保存的更改，保存吗？\r\n是：保存并退出； 否：直接退出； 取消：返回","退出",MessageBoxButtons.YesNoCancel);
                if( dr == DialogResult.Yes)
                {
                    _prj.Save();//保存
                }else if(dr == DialogResult.Cancel)
                {
                    e.Cancel = true;//取消退出
                }
            }
        }
    }
}
